#Region " Imports "

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Configuration
Imports System.Configuration.ConnectionStringsSection
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Configuration
Imports System.Web.Security
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Globalization
Imports System.Net.Configuration
Imports WebBase
Imports BA.DAL

#End Region

Partial Class Install_DBInstall
    Inherits InstallerBase

#Region " Enums, Memebers and Properties "

    Private SQLscripts As String() = New String() {"tables.sql", "functions.sql", "views.sql", "procedures.sql", "InsertBaseData.sql"}

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Me.Page.IsPostBack = False Then
            LoadSettings(Me.uxConnectionStringTextBox)
        End If

    End Sub

#End Region

#Region " Save Data "

    Public Sub SaveSettings()
        Dim configuration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        Dim AppSettingsSection As AppSettingsSection = CType(configuration.GetSection("appSettings"), AppSettingsSection)
        Dim ConnStringSection As ConnectionStringsSection = CType(configuration.GetSection("connectionStrings"), ConnectionStringsSection)

        If Not (ConnStringSection Is Nothing) Then
            Try
                If CType(System.Enum.Parse(GetType(DBUtility.Providers), Me.uxProviderRadioButtonList.SelectedValue.ToString.Trim, True), DBUtility.Providers) = BA.DAL.DBUtility.Providers.SqlServer Then
                    ConnStringSection.ConnectionStrings("BlazeApps").ConnectionString = String.Format("server={0};uID={1};pwd={2};database={3};Connect Timeout=2400;Integrated Security=false;", Me.uxDatabaseInstanceTextBox.Text.Trim, Me.uxDatabaseOwnerTextBox.Text.Trim, Me.uxDatabaseOwnerPasswordTextBox.Text.Trim, Me.uxDatabaseNameTextBox.Text.Trim)
                    ConnStringSection.ConnectionStrings("BlazeApps").ProviderName = "SqlServer"

                    If IsNothing(AppSettingsSection.Settings("DBProvider")) Then
                        AppSettingsSection.Settings.Add("DBProvider", "SqlServer")
                    Else
                        AppSettingsSection.Settings("DBProvider").Value() = "SqlServer"
                    End If
                End If

                If CType(System.Enum.Parse(GetType(DBUtility.Providers), Me.uxProviderRadioButtonList.SelectedValue.ToString.Trim, True), DBUtility.Providers) = BA.DAL.DBUtility.Providers.MySQL Then
                    ConnStringSection.ConnectionStrings("BlazeApps").ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false;", Me.uxDatabaseInstanceTextBox.Text.Trim, Me.uxDatabaseOwnerTextBox.Text.Trim, Me.uxDatabaseOwnerPasswordTextBox.Text.Trim, Me.uxDatabaseNameTextBox.Text.Trim)
                    ConnStringSection.ConnectionStrings("BlazeApps").ProviderName = "MySQL"

                    If IsNothing(AppSettingsSection.Settings("DBProvider")) Then
                        AppSettingsSection.Settings.Add("DBProvider", "MySQL")
                    Else
                        AppSettingsSection.Settings("DBProvider").Value() = "MySQL"
                    End If
                End If

                configuration.Save()
            Catch
                DisplayMessage(Resources.Resource.DBConnectionStringUpdateFailed, False, Me.uxStatusLabel)
            Finally
                DisplayMessage(Resources.Resource.DBConnectionStringUpdateSuccess, True, Me.uxStatusLabel)
            End Try

            LoadSettings(Me.uxConnectionStringTextBox)
        End If

    End Sub

#End Region

#Region " Load Data "

#End Region

#Region " Form Controls Events "

    Protected Sub uxInstallDatabaseButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxInstallDatabaseButton.Click
        Dim configuration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        Dim AppSettingsSection As AppSettingsSection = CType(configuration.GetSection("appSettings"), AppSettingsSection)
        Dim RoleSettingsSection As System.Web.Configuration.RoleManagerSection = CType(configuration.GetSection("system.web/roleManager"), System.Web.Configuration.RoleManagerSection)
        Dim MemberShipSettingsSection As System.Web.Configuration.MembershipSection = CType(configuration.GetSection("system.web/membership"), System.Web.Configuration.MembershipSection)
        Dim ProfileSettingsSection As System.Web.Configuration.ProfileSection = CType(configuration.GetSection("system.web/profile"), System.Web.Configuration.ProfileSection)
        Dim Config As New WebConfigSettings

        Dim RS As ProviderSettings = RoleSettingsSection.Providers("BlazeAppsRoleProvider")
        Dim MS As ProviderSettings = MemberShipSettingsSection.Providers("BlazeAppsProvider")
        Dim AMS As ProviderSettings = MemberShipSettingsSection.Providers("AdminProvider")
        Dim PS As ProviderSettings = ProfileSettingsSection.Providers("BlazeAppsProfileProvider")

        If ConnectionSuccess() Then

            If IsDBBuilt() Then
                Response.Redirect("DBUpgrade.aspx")
            End If

            DisplayMessage(Resources.Resource.DBInstallationStart.ToString.Trim, True, Me.uxStatusLabel)

            If ExecuteDatabaseScripts(SQLscripts) Then
                DisplayMessage(Resources.Resource.DBInstallationComplete.ToString.Trim, True, Me.uxStatusLabel)

                AppSettingsSection.Settings("AppInstalled").Value() = "True"

                If Me.uxProviderRadioButtonList.SelectedValue.Trim().ToLower() = "sqlserver" Then
                    RS.Parameters.Set("type", "System.Web.Security.SqlRoleProvider")
                    MS.Parameters.Set("type", "System.Web.Security.SqlMembershipProvider")
                    AMS.Parameters.Set("type", "System.Web.Security.SqlMembershipProvider")
                    PS.Parameters.Set("type", "System.Web.Profile.SqlProfileProvider")
                ElseIf Me.uxProviderRadioButtonList.SelectedValue.Trim().ToLower() = "mysql" Then
                    RS.Parameters.Set("type", "BA.MySqlProviders.MySqlRoleProvider")
                    MS.Parameters.Set("type", "BA.MySqlProviders.MySqlMembershipProvider")
                    AMS.Parameters.Set("type", "BA.MySqlProviders.MySqlMembershipProvider")
                    PS.Parameters.Set("type", "BA.MySqlProviders.MySqlProfileProvider")
                End If

                configuration.Save()

                Response.Redirect("../default.aspx")
            Else
                DisplayMessage(Resources.Resource.DBInstallationFailed.ToString.Trim, False, Me.uxStatusLabel)
            End If
        Else
            DisplayMessage(Resources.Resource.DBConnectionFailed.ToString.Trim, False, Me.uxStatusLabel)
        End If
    End Sub

    Protected Sub uxTestConnectionButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxTestConnectionButton.Click

        SaveSettings()

        If ConnectionSuccess() Then
            DisplayMessage(Resources.Resource.DBConnectionSuccessful, True, Me.uxStatusLabel)
        Else
            DisplayMessage(Resources.Resource.DBConnectionFailed, False, Me.uxStatusLabel)
        End If

    End Sub

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        SaveSettings()
    End Sub

#End Region

#Region " Helpers and Methods "

#End Region

End Class
